What is fast-xml-parser?
The fast-xml-parser npm package is a fast and powerful XML parser and validator that can convert XML to a JavaScript object, JSON, or a traversable JS object. It can also convert a JS object to XML. It is designed to be very fast and efficient, and it provides various options to control the XML parsing and validation process.
What are fast-xml-parser's main functionalities?
XML to JSON Conversion
Converts XML data to a JSON object. The 'parse' function takes an XML string and optional options object, and returns a JSON representation of the XML.
const { parse } = require('fast-xml-parser');
const xmlData = '<note><to>User</to><from>Library</from><heading>Reminder</heading><body>Don't forget to subscribe.</body></note>';
const options = {};
const jsonObj = parse(xmlData, options);
JSON to XML Conversion
Converts a JSON object to an XML string. The 'j2xParser' constructor takes an options object, and the 'parse' method converts the JSON object to an XML string.
const { j2xParser } = require('fast-xml-parser');
const parser = new j2xParser({});
const jsonObj = { note: { to: 'User', from: 'Library', heading: 'Reminder', body: 'Don't forget to subscribe.' } };
const xmlData = parser.parse(jsonObj);
XML Validation
Validates the XML string. The 'validate' function checks if the given XML string is well-formed and returns a validation result.
const { validate } = require('fast-xml-parser');
const xmlData = '<note><to>User</to><from>Library</from></note>';
const validationResult = validate(xmlData);
Traversable Object Creation
Creates a traversable JavaScript object from XML. The 'XMLParser' constructor creates a parser instance, and the 'parse' method converts the XML string into a traversable JS object.
const { XMLParser } = require('fast-xml-parser');
const xmlData = '<note><to>User</to><from>Library</from></note>';
const parser = new XMLParser();
const traversableObject = parser.parse(xmlData);
Other packages similar to fast-xml-parser
xml2js
xml2js is a similar npm package that provides XML to JavaScript object conversion. It includes options to customize the parsing process and supports builder options for converting back to XML. Compared to fast-xml-parser, xml2js may be slower but offers a more feature-rich API for handling complex XML structures.
xml-js
xml-js is another npm package that converts XML text to a JavaScript object and vice versa. It can also convert XML to JSON and supports compact and non-compact modes. While fast-xml-parser focuses on speed, xml-js provides a more extensive set of conversion options.
libxmljs
libxmljs is a Node.js package that provides bindings to the libxml C library. It allows for parsing and serializing XML and includes XPath and XSLT support. It is different from fast-xml-parser in that it is a binding to a native library, which may offer performance benefits and additional XML processing features.
[![NPM quality][quality-image]][quality-url]
Validate XML, Parse XML to JS Object, or Build XML from JS Object without C/C++ based libraries and no callback.
I had recently published a book, The Power Glasses. Please have a look. Your feedback would be helpful. You can mail me for a free copy.
Sponsor this project
Users
more
The list of users are mostly published by Github or communicated directly. Feel free to contact if you find any information wrong.
Main Features
- Validate XML data syntactically
- Parse XML to JS Object
- Build XML from JS Object
- Compatible to node js packages, in browser, and in CLI (click try me button above for demo)
- Faster than any other pure JS implementation.
- It can handle big files (tested up to 100mb).
- Controlled parsing using various options
- XML Entities, HTML entities, and DOCTYPE entites are supported.
- unpaired tags (Eg
<br>
in HTML), stop nodes (Eg <script>
in HTML) are supported. - You can restore almost same XML from JSON
- Supports comments
- It can preserve Order of tags in JS object
- You can control if a single tag should be parsed into array.
- Supports parsing of PI (Processing Instruction) tags with XML declaration tags
- And many more other features.
v5
I developed v5 in Apr 2023. And I didn't get the chance to complete all the features. I've ensured that new features don't impact performance. With v5, you have more control on parsing output. Check docs for syntax help and basic understanding.
Please leave a comment in discussion forum for your suggestions and if you really need v5.
How to use
To use as package dependency
$ npm install fast-xml-parser
or
$ yarn add fast-xml-parser
To use as system command
$ npm install fast-xml-parser -g
To use it on a webpage include it from a CDN
Example
As CLI command
$ fxparser some.xml
In a node js project
const { XMLParser, XMLBuilder, XMLValidator} = require("fast-xml-parser");
const parser = new XMLParser();
let jObj = parser.parse(XMLdata);
const builder = new XMLBuilder();
const xmlContent = builder.build(jObj);
In a HTML page
<script src="path/to/fxp.min.js"></script>
:
<script>
const parser = new fxparser.XMLParser();
parser.parse(xmlContent);
</script>
Bundle size
Bundle Name | Size |
---|
fxbuilder.min.js | 6.5K |
fxparser.min.js | 20K |
fxp.min.js | 26K |
fxvalidator.min.js | 5.7K |
Documents
note: version 5 is released with version 4 tfor experimental use. Based on it's demand, it'll be developed and the features can be different in final release.
Performance
negative means error
XML Parser
- Y-axis: requests per second
- X-axis: File size
XML Builder
* Y-axis: requests per second
Usage Trend
Usage Trend of fast-xml-parser
Supporters
Contributors
This project exists thanks to all the people who contribute. [Contribute].
Backers
Thank you to all our backers! 🙏 [Become a backer]
License